//
//  YTSegmentControl.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/6/2.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTSegmentedControl.h"
#import <WMPageController/WMMenuView.h>

@interface YTSegmentedControl ()<WMMenuViewDelegate, WMMenuViewDataSource>
@property (strong, nonatomic) WMMenuView *menu;
@property (strong, nonatomic) NSArray<NSString *> *titles;
@property (strong, nonatomic) YTSegmentedControlConfig *config;
@end

@implementation YTSegmentedControl

- (instancetype)initWithFrame:(CGRect)frame
                       titles:(NSArray<NSString *> *)titles
                configuration:(YTSegmentedControlConfig *)config {
    self = [super initWithFrame:frame];
    if (self) {
        _titles = titles;
        _config = config;
        _menu = [[WMMenuView alloc] initWithFrame:self.bounds];
        _menu.delegate = self;
        _menu.dataSource = self;
        _menu.style = WMMenuViewStyleLine;
        _menu.lineColor = kMainBlueColor;
        [self addSubview:_menu];
    }
    return self;
}

#pragma mark - WMMenuViewDelegate

- (void)menuView:(WMMenuView *)menu didSelesctedIndex:(NSInteger)index currentIndex:(NSInteger)currentIndex {
    if (_didSelectedItem) {
        _didSelectedItem(index);
    }
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index{
    return _config.indecatorWidth;
}

- (CGFloat)menuView:(WMMenuView *)menu titleSizeForState:(WMMenuItemState)state {
    return _config.titleSize;
}

- (UIColor *)menuView:(WMMenuView *)menu titleColorForState:(WMMenuItemState)state atIndex:(NSInteger)index {
    return state == WMMenuItemStateSelected ? kMainBlueColor : [UIColor blackColor];
}

#pragma mark - WMMenuViewDataSource

- (NSInteger)numbersOfTitlesInMenuView:(WMMenuView *)menu {
    return _titles.count;
}

- (NSString *)menuView:(WMMenuView *)menu titleAtIndex:(NSInteger)index {
    return _titles[index];
}

@end

@implementation YTSegmentedControlConfig

@end

